package com.example.es_promotion.init;

import com.example.es_promotion.entity.Promotion;
import com.example.es_promotion.service.PromotionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Arrays;

@Component
@RequiredArgsConstructor
@Slf4j
public class DataInitializer implements CommandLineRunner {
    
    private final PromotionService promotionService;
    
    @Override
    public void run(String... args) throws Exception {
        log.info("Initializing sample promotion data...");
        
        // 创建示例促销活动
        Promotion promotion1 = new Promotion();
        promotion1.setName("双十一大促");
        promotion1.setDescription("全场商品满减活动");
        promotion1.setStartTime(LocalDateTime.now().minusDays(1));
        promotion1.setEndTime(LocalDateTime.now().plusDays(30));
        promotion1.setPromotionType("DISCOUNT");
        promotion1.setApplicableSkus(Arrays.asList("SKU001", "SKU002", "SKU003"));
        promotion1.setApplicableCategories(Arrays.asList("CAT001", "CAT002"));
        promotion1.setMinPrice(50.0);
        promotion1.setMaxPrice(1000.0);
        promotion1.setStatus(1); // 已审批
        
        Promotion promotion2 = new Promotion();
        promotion2.setName("新品上市优惠");
        promotion2.setDescription("新品专属折扣");
        promotion2.setStartTime(LocalDateTime.now());
        promotion2.setEndTime(LocalDateTime.now().plusDays(15));
        promotion2.setPromotionType("NEW_PRODUCT");
        promotion2.setApplicableSkus(Arrays.asList("SKU004", "SKU005"));
        promotion2.setApplicableCategories(Arrays.asList("CAT003"));
        promotion2.setMinPrice(100.0);
        promotion2.setMaxPrice(500.0);
        promotion2.setStatus(1); // 已审批
        
        try {
            promotionService.createPromotion(promotion1);
            promotionService.createPromotion(promotion2);
            log.info("Sample promotion data initialized successfully");
        } catch (Exception e) {
            log.warn("Failed to initialize sample data: {}", e.getMessage());
        }
    }
}